<?php


namespace app\api\controller;


//cosmo.com/api/login/test
use app\api\model\Bind;
use app\api\model\Member;
use app\api\service\Login as loginService;
use think\Db;
use think\Loader;
use app\api\service\Message as messageService;


//http://cosmo.com/api/login/barcode


class Login extends BaseApi {

    #小程序登录
    public function login(){
        $code = $this->request->param('code');
        if(!$code){
            return json(['code'=>-1,'msg'=>'缺少code']);
        }
        $loginService = new loginService();
        $res = $loginService->getOpenid($code);
        return json($res);
    }

    #更新用户信息
    public function saveUserInfo(){
        Loader::import('encrypted/wxBizDataCrypt',EXTEND_PATH);
        $encryptedData = $this->request->param('data');
        $iv = $this->request->param('iv');

        $sence = intval($this->request->param('sence'));
        $type = intval($this->request->param('type'));

        $sessionKey = $this->request->param('sessionKey');
        if( empty($encryptedData) || empty($iv) || empty($sessionKey)) {
            return json(['code'=>-1,'缺少参数']);
        }

        $pc = new \WXBizDataCrypt(Config('wxinfo.appId'), $sessionKey);
        $errCode = $pc->decryptData($encryptedData,$iv,$data);
        if($errCode == 0){
            $result = json_decode($data,true);
            $member = new Member();
            $item = $member->getMember($result['openId']);
            if(!empty($item) && empty($item->avatar_image)){
                $item->nickname = $result['nickName'];
                $item->avatar_image = $result['avatarUrl'];
                $item->save();
                //生成会员码和积分码
                $item->membersn = 'CSM'.date('Y').$item->id;
                $item->creditcode = 'RA'.date('Y').$item->id;
                $item->save();
            }

            //绑定关系
            if($sence){
                $msg = $this->binding($sence,$item,$type);
                $item->msg = $msg;
            }

            return json(['code'=>1,'data'=>$item]);
        }else{
            return json(['code'=>-1,'msg'=>'用户信息更新失败']);
        }
    }

    //绑定关系
    //1.扫码成为会员(事业合伙人,销售合伙人)
    //2.扫码成为同级销售合伙人(销售合伙人)
    //3.扫码成为销售合伙人(事业合伙人)
    //4.扫码成为会员的下线(会员)
    protected function binding($secen,$item,$type){
        $qr = new \app\api\model\Qr();
        $qrItem = $qr->getQrCode(['id'=>$secen],['memberid','type','myidentity']);

        //防止二次扫描的时候出现身份混乱
        if($item->identity){
            if($item->identity == 4){ //自费的会员不能绑定上线
                return '您是已是会员,不需要在绑定了';
            }
            //2 3
            if($item->identity < $qrItem->myidentity){ //我的身份的值 < 二维码的身份值 表示我的身份低于二维码的身份
                return '您的身份是'.$this->getIdentity($item->identity).',不能扫码低级别身份';
            }
        }

        //时效
        $bind = new Bind();
        $bindItem = $bind->find();

        if($type == 1){//1.扫码成为会员(事业合伙人,销售合伙人)
            $item->identity = 3;
        } else if ($qrItem->type == 4){//扫码成为会员的下线(会员)
            $item->identity = 5;
        }

        $item->agentid = $qrItem->memberid;
        $item->bindtime = time();
        $item->periodtime = $bindItem->binds;
        $row = $item->save();
        if($row > 0){
            return '绑定成功';
        }

    }


    protected function getIdentity($identity){
        switch ($identity){
            case 1:
                return $identity = '事业合伙人';
                break;
            case 2:
                return $identity = '销售合伙人';
                break;
            case 3:
                return $identity = '会员';
                break;
            case 4:
                return $identity = '自费会员';
                break;
            default:
                return $identity = '其他';
        }
    }


}